<?php
// +----------------------------------------------------
// | Name： 星空框架  系统操作日志中间件
// +----------------------------------------------------
// | Version: V5.0
// +----------------------------------------------------
// | Author: xkyinzhe <zhonghuaxinxing@sina.cn>
// +----------------------------------------------------
// | Time: 2021-05-19 11:16
// +----------------------------------------------------
declare(strict_types=1);

namespace app\middleware;
use think\facade\Session;
use app\common\model\AdminLog as AdminLogModel;
use think\Request;

/**
 * @xk 系统操作日志中间件
 */
class SystemLog
{
  /**
   * @xk 日志写入操作
   *
   * @method handle
   * @param  Request                 $request                  [description]
   * @param  Closure                 $next                     [description]
   * @return [type]                                            [description]
   * @Author xkyinzhe                <zhonghuaxinxing@sina.cn>
   * @Time   2021-05-19T11:18:06+080
   */
  public function handle(Request $request, \Closure $next){
      // 写入系统日志
      $adminLogModel = new AdminLogModel();
      // 组合写入的条件
      $param = array();
      $param['admin_num'] = Session::has('admin_num')?Session::get('admin_num'):'未知管理账号';
      $param['admin_name'] = Session::has('admin_info')?Session::get('admin_info')['admin_name']:'未知管理员';
      $controller = $request->controller();
      $version = config('manage.version').'.';
      $controller = str_replace($version,"",$controller);
      $action = $request->action();
      $now_url = $controller.'/'.$action;
      $allow_url = [
        'index/getleftmenu',
        'login/index',
        'login/checklogin',
        'login/loginout',
        'index/index',
        'index/welcome',
        'index/getusercount',
        'index/getordercount',
        'index/getgoodscount',
        'index/getfinancecount'
      ];
      if (!in_array(strtolower($now_url), $allow_url)) {
          $param['msg'] = $now_url;
          $adminLogModel->saveLog($param);
      }
      // $param['msg'] = $now_url;
      // $adminLogModel->saveLog($param);
      return $next($request);
  }

}
